/*
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package com.example.aris;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.cordova.Config;
import org.apache.cordova.CordovaActivity;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Aris extends CordovaActivity 
{
	private EditText setUserX, setUserY;
	private EditText setBearing;
	public String[] usrLoc;
	
	private JSONObject json = new JSONObject();
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		super.init();
		// Set by <content src="index.html" /> in config.xml
		super.loadUrl(Config.getStartUrl());
		//super.loadUrl("file:///android_asset/www/index.html")
		setContentView(R.layout.main);
		setUserX = (EditText) findViewById(R.id.getUserX);
		setUserY = (EditText) findViewById(R.id.getUserY);
		setBearing = (EditText) findViewById(R.id.getUserBearing);
		//usrLoc = setUserLoc.getText().toString().split(",");
		
		/**
		 * method to deal with button click in app, sends
		 * users coordinates and bearing.
		 * 
		 *  @param view the object containing the required elements
		 *  @throws JSONException if error when building json object
		 */
	}
	
	public void process(View view) throws JSONException{
		//Intent intent = new Intent(this, DisplayMessage.class);

		//create EditTexts to retrieve the users input from the view

		//build json object for sending

		new SendData().execute();
	}

	class SendData extends AsyncTask<String, String, String>{



		@Override
		protected String doInBackground(String... args) {
			// TODO Auto-generated method stub

			try {			
				// Building Parameters

				List<NameValuePair> params = new ArrayList<NameValuePair>();

				params.add(new BasicNameValuePair("X", setUserX.getText().toString()));
				params.add(new BasicNameValuePair("Y", setUserY.getText().toString()));
				params.add(new BasicNameValuePair("Bearing", setBearing.getText().toString()));
				HttpClient httpclient = new DefaultHttpClient();
				String URL = "http://192.168.0.10/Project/makepoints.php?X="+setUserX.getText().toString()+
						"&Y="+setUserY.getText().toString()+"&Bearing="+setBearing.getText().toString();
				HttpPost httppost = new HttpPost(URL);

				//List<NameValuePair> nvp = new ArrayList<NameValuePair>(2);    
				//nvp.add(new BasicNameValuePair("json", json.toString()));
				//httppost.setHeader("Content-type", "application/json");  
				httppost.setEntity(new UrlEncodedFormEntity(params));
				HttpResponse response = httpclient.execute(httppost); 
				HttpEntity rslts = response.getEntity();
				InputStream is = rslts.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(
						is, "iso-8859-1"), 8);
				StringBuilder sb = new StringBuilder();
				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
					System.out.println("Line: "+line);
				}
				is.close();

				//System.out.println("Results: "+rslts.toString());

			}catch(Exception e){
				e.printStackTrace();

			}


			return null;
		} 

	}

}

